<template>
  <el-col class="app-v-center app-column" style="overflow-y: auto;">
    <el-row class="app-h-48px app-fill-width app-v-center pl-4 app-bb-1px-l2" style="background: #545c64" :gutter="10">
      <el-button
        size="mini"
        @click="toAddType()"
      >添加商家类型</el-button>
      <!-- <el-button
        size="mini"
      >删除</el-button> -->
    </el-row>
    <el-table
      :data="typeList"
      class="app-flex-1"
      style="width: 100%;overflow-y: auto;"
    >
      <el-table-column
        prop="name"
        align="center"
        label="类型"
      />
      <el-table-column
        align="center"
        width="200"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="isEdit? '修改商家类型' : '添加商家类型'"
      :visible.sync="addTypeDialog"
      width="50vw"
    >
      <el-form ref="form" :rules="rules" class="app-width-80p" :model="form" label-width="160px">
        <el-form-item prop="name" label="类型名称">
          <el-input v-model="form.name" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addTypeDialog = false">取 消</el-button>
        <el-button type="info" @click="addType()">确 定</el-button>
      </div>
    </el-dialog>
  </el-col>
</template>

<script>
import { getTypeList,updateType,deleteTypeById,addType } from '@/api/business'
export default {
  data: () => ({
    rules: {
      name: [
        { required: true, message: '请输入商家类型', trigger: 'blur' }
      ]
    },
    isEdit: false,
    addTypeDialog: false,
    typeList: [],
    total: 0,
    form: {
      id: null,
      name: ''
    }
  }),
  mounted() {
    this.getTypeList()
  },
  methods: {
    toAddType() {
      this.form = {
        name: ''
      }
      this.isEdit = false
      this.addTypeDialog = true
    },
    getTypeList() {
      getTypeList().then(res => {
        if (res.code === 200) {
          this.typeList = res.data
          this.total = res.data.total
        }
      })
    },
    handleEdit(index,row) {
      this.isEdit = true
      this.form = row
      this.addTypeDialog = true
    },
    handleDelete(index,row) {
      this.$confirm(`确认删除商家类型${row.name}？`,'提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTypeById({ id: row.id }).then(res => {
          if (res.code === 200) {
            this.$message({ type: 'success',message: '已删除' })
            this.pageNo = 1
            this.getTypeList()
          }
        })
      }).catch(() => {
        this.$message('已取消')
      })
    },
    addType() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            updateType(this.form).then(res => {
              if (res.code === 200) {
                this.getTypeList()
                this.$message({ type: 'success',message: '操作成功' })
                this.addTypeDialog = false
              }
            })
          } else {
            addType(this.form).then(res => {
              if (res.code === 200) {
                this.pageNo = 1
                this.getTypeList()
                this.$message({ type: 'success',message: '操作成功' })
                this.addTypeDialog = false
              }
            })
          }
        }
      })
    }
  }
}
</script>
